Function Expand-Alias {
  $content=$psise.CurrentFile.Editor.text  
  [System.Management.Automation.PsParser]::Tokenize($content, [ref] $null) |
    Where { $_.Type -eq 'Command'} |
    Sort StartLine, StartColumn -Desc | Do-Expansion
}

Function Expand-CurrentAlias {
  $CaretColumn = $psise.CurrentFile.Editor.CaretColumn  
  $content=$psise.CurrentFile.Editor.text  
  [System.Management.Automation.PsParser]::Tokenize($content, [ref] $null) |
    Where { $_.Type -eq 'Command'} |
    Where { $CaretColumn -eq $_.StartColumn -Or $CaretColumn -eq $_.EndColumn} | Do-Expansion
} 

Filter Do-Expansion {
  if($_.Content -eq '?') {
    $result = Get-Command '`?' -CommandType Alias    
  } else {
    $result = Get-Command $_.Content -CommandType Alias -ErrorAction SilentlyContinue
  }
  
  if($result) {
    $psise.CurrentFile.Editor.Select($_.StartLine,$_.StartColumn,$_.EndLine,$_.EndColumn)
    $psise.CurrentFile.Editor.InsertText($result.Definition)    
  }
}

$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Expand Alias", {Expand-Alias}, 'Ctrl+Shift+E') | Out-Null
$psise.CurrentPowerShellTab.AddOnsMenu.Submenus.Add("Expand Current Alias", {Expand-CurrentAlias}, 'Ctrl+Shift+W') | Out-Null
